package cn.koala.key.firstmaintain.controller;

import cn.koala.key.api.FirstMaintainCardBorrowOrderApi;
import cn.koala.key.common.enums.EnumKeyValue;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.firstmaintain.converter.FirstMaintainCardBorrowOrderCancelVO2DTO;
import cn.koala.key.firstmaintain.converter.FirstMaintainCardBorrowOrderOutVO2DTO;
import cn.koala.key.firstmaintain.converter.FirstMaintainCardBorrowOrderRefuselVO2DTO;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardBorrowOrderCancelDTO;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardBorrowOrderOutDTO;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardBorrowOrderRefuseDTO;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardBorrowOrderSearchParamVO;
import cn.koala.key.firstmaintain.entity.vo.*;
import cn.koala.key.firstmaintain.service.FirstMaintainCardBorrowOrderService;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultList;
import cn.seed.web.jwt.JwtTokenAuthentication;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 首保卡借用单controller
 *
 * @author liutao
 * @date 2021/1/29
 */
@RestController
@Api(value = "FirstMaintainCardBorrowOrderController", tags = "首保卡借用单")
@RequestMapping("/first/card/borrow")
public class FirstMaintainCardBorrowOrderController {

    @Autowired
    private FirstMaintainCardBorrowOrderService firstMaintainCardBorrowOrderService;

    @ApiOperation("考拉借出借用单")
    @PutMapping("out")
    public Result<?> borrowOut(@RequestBody FirstMaintainCardBorrowOrderOutVO borrowOrderOutVO,
                               HttpServletRequest request
    ) {
        String oprNo = JwtTokenAuthentication.getUserCode(request);
        String oprName = JwtTokenAuthentication.getJwtUserName(request);
        FirstMaintainCardBorrowOrderOutDTO borrowOrderOutDTO = new FirstMaintainCardBorrowOrderOutVO2DTO(oprNo, oprName).convert(borrowOrderOutVO);
        return firstMaintainCardBorrowOrderService.borrowOut(borrowOrderOutDTO);
    }


    @ApiOperation("考拉确认收到门店归还首保卡")
    @PutMapping("confirm/receive")
    public Result<?> confirmReceiveCard(@RequestParam String orderNo, HttpServletRequest request) {
        String oprNo = JwtTokenAuthentication.getUserCode(request);
        String oprName = JwtTokenAuthentication.getJwtUserName(request);
        return firstMaintainCardBorrowOrderService.confirmReceiveCard(orderNo, oprNo, oprName);
    }

    @ApiOperation("考拉拒绝门店借用首保卡")
    @PutMapping("refuse")
    public Result<?> refuse(@Valid @RequestBody FirstMaintainCardBorrowOrderRefuseVO refuseVO, HttpServletRequest request) {
        String oprNo = JwtTokenAuthentication.getUserCode(request);
        String oprName = JwtTokenAuthentication.getJwtUserName(request);
        FirstMaintainCardBorrowOrderRefuseDTO refuseDTO = new FirstMaintainCardBorrowOrderRefuselVO2DTO(oprNo, oprName).convert(refuseVO);
        return firstMaintainCardBorrowOrderService.refuseOrder(refuseDTO);
    }

    @ApiOperation("考拉取消借用首保卡")
    @PutMapping("cancel")
    public Result<?> cancel(@Valid @RequestBody FirstMaintainCardBorrowOrderCancelVO cancelVO, HttpServletRequest request) {
        String oprNo = JwtTokenAuthentication.getUserCode(request);
        String oprName = JwtTokenAuthentication.getJwtUserName(request);
        FirstMaintainCardBorrowOrderCancelDTO cancelDTO = new FirstMaintainCardBorrowOrderCancelVO2DTO(oprNo, oprName).convert(cancelVO);
        return firstMaintainCardBorrowOrderService.cancelOrder(cancelDTO);
    }

    @ApiOperation("借用单详情界面获取信息")
    @GetMapping("detail")
    public Result<FirstMaintainCardBorrowOrderDetailVO> orderDetail(@ApiParam("借用单号") @RequestParam String orderNo) {
        return firstMaintainCardBorrowOrderService.orderDetail(orderNo);
    }

    @ApiOperation("借用单借出/确认收到/取消/拒绝获取信息")
    @GetMapping("fillOrderInfo")
    public Result<FirstMaintainCardBorrowOrderFillOrderInfoVO> fillOrderInfo(@ApiParam("借用单号") @RequestParam String orderNo) {
        return firstMaintainCardBorrowOrderService.fillOrderInfo(orderNo);
    }

    @ApiOperation("借用单列表")
    @GetMapping("/search")
    public Result<ResultList<FirstMaintainCardBorrowOrderSearchResultVO>> searchByCondition(FirstMaintainCardBorrowOrderSearchParamVO paramVO) {
        return firstMaintainCardBorrowOrderService.searchByCondition(paramVO);
    }

    @ApiOperation("列出首保卡借用单状态")
    @GetMapping("status/list")
    public Result<List<EnumKeyValue>> listOrderStatus() {
        return FeignUtils.getHttpApi(FirstMaintainCardBorrowOrderApi.class).listOrderStatus();
    }

    @ApiOperation("列出首保卡借用来源")
    @GetMapping("source/list")
    public Result<List<EnumKeyValue>> listOrderSource() {
        return FeignUtils.getHttpApi(FirstMaintainCardBorrowOrderApi.class).listOrderSource();
    }
}
